home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0010_MEMINFO.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  85 lines

  1. {
  2. First of all something about Turbo Pascal memory management. A Turbo Pascal
  3. Program Uses the upper part of the memory block it allocates as the heap.
  4. The heap is the memory allocated when using the Procedures 'New' and
  5. 'GetMem'. The heap starts at the address location pointed to by 'Heaporg' and
  6. grows to higher addresses as more memory is allocated. The top of the heap,
  7. the first address of allocatable memory space above the allocated memory
  8. space, is pointed to by 'HeapPtr'.
  9.  
  10. Memory is deallocated by the Procedures 'Dispose' and 'FreeMem'. As memory
  11. blocks are deallocated more memory becomes available, but..... When a block
  12. of memory, which is not the top-most block in the heap is deallocated, a gap
  13. in the heap will appear. to keep track of these gaps Turbo Pascal maintains
  14. a so called free list.
  15.  
  16. The Function 'MaxAvail' holds the size of the largest contiguous free block
  17. _in_ the heap. The Function 'MemAvail' holds the sum of all free blocks in
  18. the heap.
  19.  
  20. Thus Far nothing has changed from TP5.5 to TP6.0. But here come the
  21. differences:
  22.  
  23. TP5.5
  24.  
  25. to keep track of the free blocks in the heap, TP5.5 maintains a free list
  26. which grows _down_ from the top of the heap. As more free blocks become
  27. available, this list will grow. Every item in this list, a free-list Record,
  28. contains two four-Byte Pointers to the top and the bottom of a free block
  29. in the heap. _FreePtr_ points to the first free-list Record (the bottom most
  30. free-list Record).
  31.  
  32. The minimum _allowable_ distance between 'FreePtr' and 'HeapPtr' can be set
  33. with the Variable 'FreeMin'.
  34.  
  35. TP6.0
  36.  
  37. In TP6.0 the Variables 'FreePtr' and 'FreeMin' no longer exist. The free list
  38. as implemented in TP5.5 no longer exists either (although the TP6.0
  39. Programmer's guide still mentions a down growing free list??)). TP6.0 keeps
  40. track of the free blocks by writing a 'free list Record' to the first eight
  41. Bytes of the freed memory block! A (TP6.0) free-list Record contains two four
  42. Byte Pointers of which the first one points to the next free memory block, the
  43. second Pointer is not a Real Pointer but contains the size of the memory block.
  44. Summary
  45.  
  46. So instead of a list of 'free list Records', growing down from the top of the
  47. heap, containing Pointers to individual memory blocks, TP6.0 maintains a linked
  48. list With block sizes and Pointers to the _next_ free block.
  49. In TP6.0 an extra heap Variable 'Heapend' designating the end of the heap is
  50. added. When 'HeapPtr' and 'FreeList' have the same value, the free list is
  51. empty.
  52.  
  53. The below figure pictures the memory organization of both TP5.5 and TP6.0:
  54.  
  55.  
  56.   TP5.5              TP6.0     Heapend
  57. ───          ┌─────────┐                 ┌─────────┐ <────
  58.  ^    ┌──────│         │                 │         │
  59.  │    │      ├─────────┤                 │         │
  60.  │    │  ┌── │         │  FreePtr        │         │
  61.  │    │  │   ├─────────┤ <────           │         │
  62. Heap  │  │   │         │                 │         │
  63.  │    │  │   │         │                 │         │
  64.  │    │  │   │         │                 │         │
  65.  v    │  │   │         │  HeapPtr        │         │  HeapPtr
  66. ───   │  │   ├─────────┤ <────        ┌─>├─────────┤ <────
  67.       │  │   │         │              │  │         │
  68.       │  ├──>├─────────┤              │  ├─────────┤
  69.       │  │   │  Free   │              └──│  Free   │
  70.       │  └──>├─────────┤              ┌─>├─────────┤
  71.       │      │         │              │  │         │
  72.       ├─────>├─────────┤              │  ├─────────┤
  73.       │      │  Free   │  Heaporg     └──│  Free   │  FreeList
  74.       └─────>├─────────┤ <────           ├─────────┤ <────
  75.                                          │         │  Heaporg
  76.                                       ├─────────┤ <────
  77.  
  78.  
  79.  
  80.  
  81.  
  82. I hope this will help you modifying existing toolBox's which make use of these
  83. disappeared Variables. In some Case a modification may be quite easy, but as
  84. you see it might get quite quite difficult as well.
  85.